import React, { useState, useEffect } from 'react';
import { Title } from 'base/ctl/title';
import { loc } from 'local';
import { g_wsn } from 'db/state';
import { Time } from 'base/ctl/base';
import { uDb, cline, GridTable, useHigh, for_obj, obj_str, loc_date, is_empty, Wait, save_xlsx, d_sort, NuiBt } from 'sui';
import { get_qr_b } from './cmd';
import { adjust_val } from '../qr/cmd';

const PairView = ({ hreturn, cur, title }) => {
    const [main, setMain] = useState();
    const [wait, setWait] = useState(false);
    const [high] = useHigh(130, 200)

    useEffect(() => {
        const sn = obj_str(cur, "sn");
        if (!sn) {
            return;
        }

        uDb(null, "psn/list",
            {
                line: cline(),
                k: sn
            },
            r => {
                adjust_val(r);

                const tmp = {};

                r.forEach(d => {
                    const groupsn = obj_str(d, "groupsn");
                    const gtp = obj_str(d, "gtp");

                    if (!tmp[groupsn]) {
                        tmp[groupsn] = {};
                    }

                    tmp[groupsn][gtp] = d;
                });

                const val = [];

                for_obj(tmp, (k, v) => {
                    const aval = v["A"];
                    const bval = v["B"];

                    const d = aval;
                    const tech = obj_str(d, "tech");

                    d["tech"] = `${d.tech} DT`;

                    d["kia"] = obj_str(aval, "kia");
                    d["sia"] = obj_str(aval, "sia");
                    d["kea"] = obj_str(aval, "kea");
                    d["sea"] = obj_str(aval, "kea");

                    d["b_sn"] = obj_str(bval, "sn");
                    d["b_gtp"] = obj_str(bval, "gtp");
                    d["b_outdia"] = obj_str(bval, "outdia");
                    d["b_india"] = obj_str(bval, "india");
                    d["b_outwidth"] = obj_str(bval, "outwidth");
                    d["b_standout"] = obj_str(bval, "standout");
                    d["b_dent"] = obj_str(bval, "dent");
                    d["b_angle"] = obj_str(bval, "angle");

                    d["b_kia"] = obj_str(bval, "kia");
                    d["b_sia"] = obj_str(bval, "sia");
                    d["b_kea"] = obj_str(bval, "kea");
                    d["b_sea"] = obj_str(bval, "kea");

                    const a_qr = get_qr_b(aval);
                    const b_qr = get_qr_b(bval);

                    const tmp = tech.split("-");

                    const a = tmp[0];
                    let b = "";

                    if (tmp.length === 2) {
                        b = tmp[1].padEnd(5);
                    }

                    d["qr"] = `${a}-${b}DT${d.groupsn}${a_qr}${b_qr}${bval.standout}${bval.dent}${aval.standout}`;

                    val.push(d);
                });

                d_sort(val, "sn");

                setMain(val);
            }, e => setMain(null));
    }, [cur]);

    const col = [
        {name: "型号", key: 'tech', width: 120},
        {name: "类别", key: 'gtp'},
        {name: "组配编号", key: 'groupsn'},
        {name: "k1", key: 'k1'},
        {name: "k2", key: 'k2'},
        {name: "k3", key: 'k3'},
        {name: "A编号", key: 'sn', width: 120},
        {name: "D", key: 'outdia'},
        {name: "d", key: 'india'},
        {name: "k4", key: 'k4'},
        {name: "k5", key: 'k5'},
        {name: "k6", key: 'k6'},
        {name: "内圈径向跳动", key: 'kia', width: 120},
        {name: "内圈轴向跳动", key: 'sia', width: 120},
        {name: "外圈径向跳动", key: 'kea', width: 120},
        {name: "外圈轴向跳动", key: 'sea', width: 120},
        {name: "标签C", key: 'outwidth'},
        {name: "标签b", key: 'standout'},
        {name: "标签接触角", key: 'angle'},
        {name: "k7", key: 'k7'},
        {name: "类别", key: 'b_gtp'},
        {name: "k8", key: 'k8'},
        {name: "k9", key: 'k9'},
        {name: "k10", key: 'k10'},
        {name: "k11", key: 'k11'},
        {name: "B编号", key: 'b_sn', width: 120},
        {name: "D", key: 'b_outdia'},
        {name: "d", key: 'b_india'},
        {name: "k12", key: 'k12'},
        {name: "k13", key: 'k13'},
        {name: "k14", key: 'k14'},
        {name: "k15", key: 'k15'},
        {name: "内圈径向跳动", key: 'b_kia', width: 120},
        {name: "内圈轴向跳动", key: 'b_sia', width: 120},
        {name: "外圈径向跳动", key: 'b_kea', width: 120},
        {name: "外圈轴向跳动", key: 'b_sea', width: 120},
        {name: "标签C", key: 'b_outwidth'},
        {name: "标签b", key: 'b_standout'},
        {name: "标签f", key: 'b_dent'},
        {name: "标签接触角", key: 'b_angle', width: 120},
        {name: "二维码", key: 'qr', width: 500},
    ];

    const hout = () => {
        if (is_empty(main)) {
            alert("数据为空");
        }

        setWait(true);

        const head = [];

        const h = col.map(d => {
            head.push(d.key);

            return { value: d.name };
        });

        const row = [h];
        const str_h = ["tech", "gtp", "groupsn", "sn", "b_gtp", "k10", "k11", "b_sn", "qr",
            "kia", "sia", "kea", "sea", "b_kia", "b_sia", "b_kea", "b_sea", "angle", "b_angle"];
        const set = new Set();
        str_h.forEach(d => set.add(d));

        main.forEach(d => {
            const tmp = [];

            head.forEach(t => {
                let val = obj_str(d, t);
                if (set.has(t)) {
                    tmp.push({ value: val });
                    return;
                }

                const f = parseFloat(val);
                if (f > 0) {
                    val = `+${val}`;
                }

                tmp.push({ value: val });
            });

            row.push(tmp);
        });

        const tech = obj_str(cur, "tech");

        save_xlsx(row, `${tech}_${title}_${loc_date()}.xlsx`, () => setWait(false));
    };

    return (
        <div className="flex flex-col ml-2 mr-2">
            <div className="inline-flex items-center justify-between mt-2 mb-2">
                <div className="inline-flex">
                    <Title label={loc("base_43")} value={g_wsn()} width="w-56"/>
                    <Title label={title} width="w-56"/>
                </div>
                <Time/>
                <div className="inline-flex">
                    <NuiBt color="green" className="h-16 mr-4" hcmd={hout}>导出</NuiBt>
                    <NuiBt className="h-16 mr-2" hcmd={hreturn}>返回</NuiBt>
                </div>
            </div>
            <GridTable col={col} row={main} high={high}/>
            <Wait wait={wait}/>
        </div>
    );
};

export {PairView};
